package com.quanshu.shop.person.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.main.api.ShopMainIF;
import com.quanshu.shop.order.entity.SubmitPrepareEntity;
import com.quanshu.shop.person.adapter.Address1Adapter;
import com.quanshu.shop.person.custom.DeleteAddressDialog;
import com.quanshu.shop.person.entity.AdressResultEntity;
import com.quanshu.shop.person.entity.MySelectAddressEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyAdressActivity extends BaseActivity {
    @BindView(R.id.my_address_title)
    BaseTitleBar baseTitleBar;
    @BindView(R.id.my_address_tips)
    TextView myAddressTips;
    @BindView(R.id.my_address_rl)
    RecyclerView myAddressRl;
    @BindView(R.id.go_add_address)
    RelativeLayout goAddAddress;
    private Address1Adapter address1Adapter;
    private List<MySelectAddressEntity> mySelectAddressEntityList;
    private MySelectAddressEntity editingMySelectAddressEntity;
    private DeleteAddressDialog deleteAddressDialog;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == AddAddressActivity.ADDRESS_RESULT_CODE) {
            MySelectAddressEntity mySelectAddressEntity = (MySelectAddressEntity) data.getExtras().getSerializable("result_data");
            if (requestCode == SelectMyAddressActivity.ADD_ADDRESS) {
                if (Integer.parseInt(mySelectAddressEntity.getAddressListEntity().getIs_default()) == 1) {
                    setDefaultfalse();
                }
                mySelectAddressEntityList.add(mySelectAddressEntity);
                address1Adapter.notifyDataSetChanged();
            } else if (requestCode == SelectMyAddressActivity.EDIT_ADDRESS) {
                for (int a = 0; a < mySelectAddressEntityList.size(); a++) {
                    if (Integer.parseInt(mySelectAddressEntityList.get(a).getAddressListEntity().getId()) == Integer.parseInt(mySelectAddressEntity.getAddressListEntity().getId())) {
                        setDefaultfalse();
                        mySelectAddressEntityList.get(a).setAddressListEntity(mySelectAddressEntity.getAddressListEntity());
                        if (Integer.parseInt(editingMySelectAddressEntity.getAddressListEntity().getIs_default()) == 1) {
                            if (Integer.parseInt(mySelectAddressEntity.getAddressListEntity().getIs_default()) == 0) {
                                setDefault(0, mySelectAddressEntityList.get(0));
                            }

                        } else {
                            if (Integer.parseInt(mySelectAddressEntity.getAddressListEntity().getIs_default()) == 1) {
                                setDefaultfalse();
                                mySelectAddressEntityList.get(a).getAddressListEntity().setIs_default("1");
                            }
                        }
                        address1Adapter.notifyDataSetChanged();
                    }
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_adress);
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true).titleBar(baseTitleBar).init();
        initView();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        deleteAddressDialog = new DeleteAddressDialog(this).setOnDeleteAddressListener(new DeleteAddressDialog.OnDeleteAddressListener() {
            @Override
            public void isDelete(boolean i, int position2, MySelectAddressEntity mySelectAddressEntity2) {
                if (i) {
                    deleteAddress(position2, mySelectAddressEntity2);
                }
            }
        });
        baseTitleBar.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("收货地址").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        mySelectAddressEntityList = new ArrayList<>();
        RxViewHelper.getInstance().clicks(goAddAddress, aVoid -> {
                    Intent intent = new Intent();
                    intent.setClass(MyAdressActivity.this, AddAddressActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("title", "添加地址");
                    bundle.putString("request_code", String.valueOf(SelectMyAddressActivity.ADD_ADDRESS));
                    intent.putExtras(bundle);
                    startActivityForResult(intent, SelectMyAddressActivity.ADD_ADDRESS);
                }
        );
        List<String> stringList = new ArrayList<>();
        for (int a = 0; a < 10; a++) {
            stringList.add("women");
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        myAddressRl.setLayoutManager(linearLayoutManager);
        getMyAddress();
    }

    private void getMyAddress() {
        NetClient.getInstance().getShopService(ShopMainIF.class)
                .getUserAddress("", currentUser.getId())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<SubmitPrepareEntity.AddressListEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<SubmitPrepareEntity.AddressListEntity>> response) {
                        for (int a = 0; a < response.getData().size(); a++) {
                            MySelectAddressEntity mySelectAddressEntity = new MySelectAddressEntity();
                            mySelectAddressEntity.setAddressListEntity(response.getData().get(a));

                            mySelectAddressEntityList.add(mySelectAddressEntity);
                        }
                        address1Adapter = new Address1Adapter(mySelectAddressEntityList);
                        address1Adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                switch (view.getId()) {
                                    case R.id.set_default:
                                        if (Integer.parseInt(mySelectAddressEntityList.get(position).getAddressListEntity().getIs_default()) == 0) {
                                            //设置默认地址
                                            setDefault(position, mySelectAddressEntityList.get(position));
                                        }
                                        break;
                                    case R.id.ll_edit:
                                        Intent intent = new Intent();
                                        intent.setClass(MyAdressActivity.this, AddAddressActivity.class);
                                        Bundle bundle = new Bundle();
                                        bundle.putString("title", "编辑地址");
                                        bundle.putString("request_code", String.valueOf(SelectMyAddressActivity.EDIT_ADDRESS));
                                        bundle.putSerializable("request_data", mySelectAddressEntityList.get(position));
                                        intent.putExtras(bundle);
                                        editingMySelectAddressEntity = new MySelectAddressEntity();
                                        SubmitPrepareEntity.AddressListEntity addressListEntity = new SubmitPrepareEntity.AddressListEntity();
                                        addressListEntity.setIs_default(mySelectAddressEntityList.get(position).getAddressListEntity().getIs_default());
                                        editingMySelectAddressEntity.setAddressListEntity(addressListEntity);
                                        startActivityForResult(intent, SelectMyAddressActivity.EDIT_ADDRESS);
                                        break;
                                    case R.id.ll_delete:
                                        deleteAddressDialog.show(position, mySelectAddressEntityList.get(position));
                                        break;
                                }
                                address1Adapter.notifyDataSetChanged();
                            }
                        });
                        myAddressRl.setAdapter(address1Adapter);
                    }
                });
    }

    private void setDefault(int position, MySelectAddressEntity mySelectAddressEntity) {
        NetClient.getInstance().getShopService(ShopMainIF.class)
                .addAddress(AppUtils.getSignParams(currentUser.getId(), mySelectAddressEntity.getAddressListEntity().getId(), mySelectAddressEntity.getAddressListEntity().getAccept_name(), mySelectAddressEntity.getAddressListEntity().getMobile(), mySelectAddressEntity.getAddressListEntity().getProvince(), mySelectAddressEntity.getAddressListEntity().getCity(), mySelectAddressEntity.getAddressListEntity().getArea(), mySelectAddressEntity.getAddressListEntity().getAddress(), "1"), currentUser.getId(), mySelectAddressEntity.getAddressListEntity().getId(), mySelectAddressEntity.getAddressListEntity().getAccept_name(), mySelectAddressEntity.getAddressListEntity().getMobile(), mySelectAddressEntity.getAddressListEntity().getProvince(), mySelectAddressEntity.getAddressListEntity().getCity(), mySelectAddressEntity.getAddressListEntity().getArea(), mySelectAddressEntity.getAddressListEntity().getAddress(), "1")
                .compose(new BaseTransformer<>(this, true, true))
                .subscribe(new BaseObserver<BaseResponse<AdressResultEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<AdressResultEntity> response) {
                        ToastHelper.toast("设置成功！");
                        setDefaultfalse();
                        mySelectAddressEntityList.get(position).getAddressListEntity().setIs_default("1");
                        address1Adapter.notifyDataSetChanged();
                    }
                });
    }

    private void setDefaultfalse() {
        for (int a = 0; a < mySelectAddressEntityList.size(); a++) {
            mySelectAddressEntityList.get(a).getAddressListEntity().setIs_default("0");
        }
    }


    private void deleteAddress(int position, MySelectAddressEntity mySelectAddressEntity) {
        NetClient.getInstance().getShopService(ShopMainIF.class)
                .deleteUserAddress(AppUtils.getSignParams(currentUser.getId(), mySelectAddressEntity.getAddressListEntity().getId()), currentUser.getId(), mySelectAddressEntity.getAddressListEntity().getId())
                .compose(new BaseTransformer<>(this, true, true))
                .subscribe(new BaseObserver<BaseResponse<String>>() {
                    @Override
                    public void onSuccess(BaseResponse<String> response) {
                        ToastHelper.toast("删除成功！");
                        mySelectAddressEntityList.remove(position);
                        if (mySelectAddressEntityList.size() <= 0) {
                        } else {
                            if (mySelectAddressEntity.isSelect()) {
                                mySelectAddressEntityList.get(0).setSelect(true);
                            }
                            if (Integer.parseInt(mySelectAddressEntity.getAddressListEntity().getIs_default()) == 1) {
                                mySelectAddressEntityList.get(0).getAddressListEntity().setIs_default("1");
                            }
                        }
                        address1Adapter.notifyDataSetChanged();
                    }
                });
    }
}

